var  CITEDOM=new ActiveXObject("Msxml2.DOMDocument");


function import_allcitations() {
  var allfiles=dos_dir(GetPrefsCaesarDir(),".xml");
  var progbar = new html_progbar("Importing "+allfiles.length+" files",allfiles.length);
  var activedossier=GetDossierNumber();

  for(f in allfiles) {
    progbar.message(allfiles[f]);
    import_citations(allfiles[f]);
    progbar.advance();
  }
  progbar.close();
  LoadDossier(activedossier); // restore active dossier 
}

function import_citations(fname,opt) {
  var nbimp=0;
  var docs="";
  if(!dos_isfile(GetPrefsCaesarDir()+fname+".xml")) {
    //alert("File "+fname +" does not exist");
    return;
  }
  CITEDOM.load(GetPrefsCaesarDir()+fname+".xml");
  // Inport misc info on dossier 
  var dosroot=CITEDOM.selectSingleNode("/dossier");

  if(!opt) {
    LoadDossier(fname,"quiet");
  }

  SetDossierLang(dosroot.getAttribute("language"));
  SetDossierTitle(dosroot.selectSingleNode("titles/title[ @language='"+GetDossierLang()+"']").text);
  SetDossierApplicant(dosroot.selectSingleNode("applicants/applicant/participant/name").text);

  if(opt) {
    return;
  }

  SaveDossier(fname);

  var citetab=CITEDOM.selectNodes("/dossier/citations/citeddocument");
  if(citetab) {
    for(var i=0; i < citetab.length; i++) {
      switch(citetab[i].getAttribute("type")) {
      case "Patent":
	var num=citetab[i].selectSingleNode('properties/property [@name="number"]');
	var off=citetab[i].selectSingleNode('properties/property [@name="office"]');
	var txt=citetab[i].selectSingleNode('usercomment');
	if(num && num.text && off && off.text && txt && txt.text) {
	  SetDocDBRmks(off.text+num.text,fname,txt.text);
	  nbimp++;
	  docs+=off.text+num.text+" ";
	}
	break;
      default:
	var title =citetab[i].selectSingleNode('properties/property [@name="title" ]');
	var title2=citetab[i].selectSingleNode('properties/property [@name="formattedText" ]');
	var xp=citetab[i].selectSingleNode('properties/property [@name="xpNumber" ]');
	var txt=citetab[i].selectSingleNode('usercomment');
	if(xp && xp.text && txt && txt.text) {
	  SetDocDBRmks("XP"+xp.text,fname,txt.text);
	  nbimp++;
	  docs+="XP"+xp.text+" ";
	}
       break;
     }
   }
  } else {
    alert("Wrong file "+fname);
  }
  if(nbimp>0) {
    //alert("Imported "+docs+" ("+ nbimp+") from "+fname);
  } 
}

